<script setup name="AnnualFeeDbl">
import { ElMessageBox } from 'element-plus'
import Message from 'vue-m-message'
import YearFeeDetailModal from '@/views/system/annualFee/components/YearFeeDetailModal.vue'
import annualFeeService from '@/api/annualFee/index.js'
import Toolbar from '@/components/Toolbar/Toolbar.vue'
import { tableCheckChane } from '@/utils/index.js'
import { yearFeeSearchConfig, yearFeeTableColumns } from '@/views/system/annualFee/yearFeeConfig.js'

const { proxy } = getCurrentInstance()
const pagerConfig = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10,
})
const ids = ref([])
const tableData = ref([])
const queryParam = ref({
  feeNode: '0',
})
const annualFeeId = ref('')
const yearFeeDetailModalVisible = ref(false)
const detailParams = ref({})
const searchConfig = ref(getSearchConfig())
const uploadVisible = ref(false)
const uploadData = ref({})

onMounted(() => {
  getList()
})

function getSearchConfig() {
  return yearFeeSearchConfig.map((item) => {
    const newItem = { ...item }
    if (item.field !== 'feeNode') {
      // newItem.default = undefined; // 直接设置 default 属性为 undefined
    }
    else {
      newItem.default = queryParam.value.feeNode
    }
    return newItem
  })
}

function getList() {
  const params = {
    pageSize: pagerConfig.value.pageSize,
    pageNum: pagerConfig.value.currentPage,
    status: '3',
    ...queryParam.value,
  }
  annualFeeService.list(params).then((res) => {
    const { rows, total } = res
    pagerConfig.value.total = total
    tableData.value = rows
  })
}

function handleView(val) {
  detailParams.value = val
  yearFeeDetailModalVisible.value = true
}

function handlePageChange({ currentPage, pageSize }) {
  pagerConfig.value.currentPage = currentPage
  pagerConfig.value.pageSize = pageSize
  getList()
}

function handleCheckChange(data) {
  ids.value = tableCheckChane(data, 'annualfeeId', ids.value)
}

function handleExport() {
  const annualfeeIds = ids.value.join(',') || tableData.value.map(item => item.annualfeeId).join(',')
  proxy.download('', { ...queryParam.value, annualfeeIds }, `annualFeeDbl__${new Date().getTime()}.xlsx`)
}

function handleSearch(params) {
  queryParam.value = {
    ...params,
    currentPage: 1,
  }
  getList()
}

function handleTabChange(name) {
  queryParam.value = {}
  pagerConfig.value.currentPage = 1
  pagerConfig.value.pageSize = 10
  queryParam.value.feeNode = name
  searchConfig.value = getSearchConfig()
  // searchConfig.value = searchConfig.value.map((item) => {
  //   item.default = ''
  //   if (item.field === 'feeNode') {
  //     item.default = name
  //   }
  //   return item
  // })
  getList()
}

function handleEditRemark(row) {
  const { remark } = row
  ElMessageBox.prompt('请输入备注', '提示', {
    inputType: 'textarea',
    inputValue: remark,
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    beforeClose: (action, instance, done) => {
      if (action === 'confirm') {
        annualFeeService.update({ ...row, remark: instance.inputValue, isUpdateRemark: '1' }).then((res) => {
          const code = res.code
          if (code === 200) {
            Message.success('修改成功')
            done()
          }
        })
      }
      else {
        done()
      }
    },
  }).then((res) => {
    getList()
  })
}

function handleUpload(scope) {
  uploadData.value.annualfeeId = scope.annualfeeId
  uploadVisible.value = true
}

function handleUploadSubmit() {
  annualFeeService.update(uploadData.value).then((res) => {
    const code = res.code
    if (code === 200) {
      Message.success('上传成功')
      handleDialogCancel()
      getList()
    }
  })
}

function handleDialogCancel() {
  uploadVisible.value = false
  uploadData.value = {}
}
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="">
      <el-tabs v-model="queryParam.feeNode" @tab-change="handleTabChange">
        <el-tab-pane label="办登" name="0" />
        <el-tab-pane label="年费" name="1" />
        <el-tab-pane label="滞纳金" name="2" />
        <el-tab-pane label="恢复金" name="3" />
      </el-tabs>
      <SearchBar :search-config="searchConfig" @search="handleSearch" />
    </el-card>
    <el-card shadow="never" class="mt-2">
      <Toolbar>
        <el-button
          v-hasPermi="['system:annualFee:export']"
          type="warning"
          class="el-gradient-warning"
          @click="handleExport()"
        >
          导出
          <el-icon class="el-icon--right">
            <FolderAdd />
          </el-icon>
        </el-button>
      </Toolbar>
      <vxe-grid
        :row-config="{
          keyField: 'annualfeeId',
        }"
        :data="tableData"
        :columns="yearFeeTableColumns"
        :pager-config="pagerConfig"
        header-cell-class-name="headerCellClass"
        max-height="500px"
        @page-change="handlePageChange"
        @checkbox-all="handleCheckChange"
        @checkbox-change="handleCheckChange"
      >
        <template #actions="{ row }">
          <el-button
            v-hasPermi="['system:annualFee:query']"
            size="small"
            text
            @click="handleView(row)"
          >
            查看
          </el-button>
          <el-button
            v-hasPermi="['system:annualFee:edit']"
            size="small"
            text
            type="primary"
            @click="handleEditRemark(row)"
          >
            修改备注
          </el-button>
          <el-button
            v-hasPermi="['system:annualFee:edit']"
            size="small"
            type="success"
            text
            @click="handleUpload(row)"
          >
            上传
          </el-button>
        </template>
      </vxe-grid>
    </el-card>
    <YearFeeDetailModal
      v-model:visible="yearFeeDetailModalVisible"
      :detail-params="detailParams"
    />
    <el-dialog
      v-model="uploadVisible"
      class="custom-dialog"
      width="30%"
      @close="handleDialogCancel"
    >
      <template #header>
        <div class="dialog-header">
          <span class="relative top-3 text-[16px]">
            上传凭证
          </span>
        </div>
      </template>
      <el-alert title="提交收据后无法修改，请仔细核对！" type="warning" show-icon :closable="false" class="mb-4!" />
      <FileUpload v-model="uploadData.filePath" :file-type="['pdf', 'jpg', 'jpeg', 'png']" :limit="1" />
      <template #footer>
        <div class="px-4 py-2">
          <el-button type="primary" :disabled="!uploadData.filePath" @click="handleUploadSubmit">
            上传
          </el-button>
          <el-button @click="handleDialogCancel">
            取消
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
.custom-message-box {
  .el-message-box__message {
    width: 100%;
  }
}
</style>
